<?php
namespace App\Http\Controllers\Home;
use App\http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\App;
use Illuminate\Http\Request;
class IndexController extends BaseController{
    //首页
    public function index(){
        $this -> go_login();
        //查询广告图
        $banner1=DB::table('banner')->limit(3)->get();
        $banner2=DB::table('banner')->offset(3)->limit(4)->get();
        //推荐商品
        $pick_godds=DB::table('good as g')
            ->select("g.*","c.classify_name")
            ->leftJoin('classify as c',"g.classify_id","c.classify_id")
            ->whereRaw("recommand=1 AND isnew=1")->limit(1)->get();
        $pick_godds2=DB::table('good')->whereRaw("recommand=1 AND isnew=1")->orderBy("good_addtime","ASC")->limit(1)->first();
        $pick_godds2 -> good_dec = msubstr($pick_godds2 -> good_dec ,0,140,"utf-8",true);
        //特色产品
        $goods_tese=DB::table('good as g')
            ->select("g.*","c.classify_name")
            ->leftJoin('classify as c',"g.classify_id","c.classify_id")
            ->limit(10)->get();
        $this->user_name = Cookie::get('u_name');
        $user_name = $this->user_name;
        $user_id = DB::table('user')->where('user_name', $user_name)->first();
        $user_id = $user_id->user_id;
        foreach($goods_tese as $k=> $v){
            $wish = Db::table('wish')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($wish) {
                $goods_tese[$k]->color = 'wishColor';
            } else {
                $goods_tese[$k]->color = '1';
            }
            $shopcar = Db::table('shopcar')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($shopcar) {
                $goods_tese[$k]->cartColor = 'cartColor';
                // $cartColor = 'cartColor';
            } else {
                $goods_tese[$k]->cartColor = '1';
                // $cartColor = '1';
            }
        }
        //在Hurst上购买
        //新品上市
        $goods_news=DB::table('good as g')
            ->select("g.*","c.classify_name")
            ->leftJoin('classify as c',"g.classify_id","c.classify_id")
            ->orderBy("good_addtime","ASC")
            ->limit(8)->get();
        foreach($goods_news as $k=> $v){
            $wish = Db::table('wish')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($wish) {
                $goods_news[$k]->color = 'wishColor';
            } else {
                $goods_news[$k]->color = '1';
            }
            $shopcar = Db::table('shopcar')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($shopcar) {
                $goods_news[$k]->cartColor = 'cartColor';
                // $cartColor = 'cartColor';
            } else {
                $goods_news[$k]->cartColor = '1';
                // $cartColor = '1';
            }

        }
        //热卖推荐
        $goods_hot=DB::table('good as g')
            ->select("g.*","c.classify_name")
            ->leftJoin('classify as c',"g.classify_id","c.classify_id")
            ->orderBy("good_addtime","DESC")
            ->limit(8)->get();
        foreach($goods_hot as $k=> $v){
            $wish = Db::table('wish')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($wish) {
                $goods_hot[$k]->color = 'wishColor';
            } else {
                $goods_hot[$k]->color = '1';
            }
            $shopcar = Db::table('shopcar')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($shopcar) {
                $goods_hot[$k]->cartColor = 'cartColor';
                // $cartColor = 'cartColor';
            } else {
                $goods_hot[$k]->cartColor = '1';
                // $cartColor = '1';
            }

        }
        //匠心独运
        $goods_chair=DB::table('good as g')
            ->select("g.*","c.classify_name")
            ->leftJoin('classify as c',"g.classify_id","c.classify_id")
            ->where("g.classify_id","12")
            ->orderBy("good_addtime","ASC")
            ->limit(8)->get();
        foreach($goods_chair as $k=> $v){
            $wish = Db::table('wish')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($wish) {
                $goods_chair[$k]->color = 'wishColor';
            } else {
                $goods_chair[$k]->color = '1';
            }
            $shopcar = Db::table('shopcar')->where(["good_id"=>$v->good_id,"user_id"=>$user_id])->first();
            if ($shopcar) {
                $goods_chair[$k]->cartColor = 'cartColor';
                // $cartColor = 'cartColor';
            } else {
                $goods_chair[$k]->cartColor = '1';
                // $cartColor = '1';
            }

        }
        //博客
        $blog=DB::table('blog')->orderBy("blog_time","ASC")->limit(2)->get();
        // foreach($blog as $k=>$v){
        //     $blog_time=$v->blog_time;
        //     $blog_time=date('Y-m-d H:i:s',$blog_time);
        //     $blog[$k]->blog_time=$blog_time;
        // }
        //心愿单

        //是否是加入购物车

        $arr=array(
            'banner1'=>$banner1,
            'banner2'=>$banner2,
            'pick_godds'=>$pick_godds,
            'pick_godds2'=>$pick_godds2,
            'goods_tese'=>$goods_tese,
            'blog'=>$blog,
            'goods_news'=>$goods_news,
            'goods_hot'=>$goods_hot,
            'goods_chair'=>$goods_chair,

        );
// dd($banner1);
        return view('home/index',$arr);

    }

    //关于我们
    public function about(){
        $this->go_login();
        $team_info = Db::table('team_info') -> get();
        foreach ($team_info as $k => $data) {
            $team_path = $data -> team_path;
            $team_path = $team_path == ''?'/static/home/img/team/1.png':'/uploads/user/'.$team_path;
            $team_info[$k] -> team_path = $team_path;
        }
        $data = array(
            'team_info' => $team_info,
        );
        return view('home/about',$data);
    }


    //联系我们
    public function contact(){
        $this->go_login();
        return view('home/contact');
    }
    //404错误页面
    public function error(){
        $this->go_login();
        return view('home/error');
    }

    //个人信息
    public function my_account(){
        $this->go_login();
        $user = Db::table('user') -> get();
        $data = array(
            'user' => $user,
        );
        return view('home/my_account',$data);
    }
    //用户个人信息
    public function user_info(Request $request){
        $this->go_login();

        //判断是否有cookie
        $iscookie=Cookie::has('u_name');
        if($iscookie){//有cookie
            //接收cookie值
            $this->user_name=Cookie::get('u_name');
            $this->user_time=Cookie::get('u_time');

            //修改最后登录时间
            $data=array(
                'user_addtime'=>$this->user_time,
            );
            DB::table('user')->where("user_name",$this->user_name)->update($data);
            //重新设置cookie有效时间
            Cookie::queue('u_name',$this->user_name,60);//60分钟
            Cookie::queue('u_time',time(),60);//最后操作的时间

            $user_name = $this->user_name;
            $user_id = DB::table('user') -> where("user_name",$user_name) -> first();
            $user_id = $user_id -> user_name == $user_name?$user_id->user_id:'';
            $user_info = DB::table('user') -> where("user_id",$user_id) -> first();
            return json_encode($user_info);

        }else{ //cookie失效,1小时没有动
            //删除session
            Session::forget('go_login');
            return 2;exit();
        }
    }
    //用户个人信息
    public function user_info2(Request $request)
    {
        $this->go_login();

        //判断是否有cookie
        $iscookie = Cookie::has('u_name');
        if ($iscookie) {//有cookie
            //接收cookie值
            $this->user_name = Cookie::get('u_name');
            $this->user_time = Cookie::get('u_time');

            //修改最后登录时间
            $data = array(
                'user_addtime' => $this->user_time,
            );
            DB::table('user')->where("user_name", $this->user_name)->update($data);
            //重新设置cookie有效时间
            Cookie::queue('u_name', $this->user_name, 60);//60分钟
            Cookie::queue('u_time', time(), 60);//最后操作的时间

            $user_name = $this->user_name;
            $user_id = DB::table('user')->where("user_name", $user_name)->first();
            $user_id = $user_id->user_name == $user_name ? $user_id->user_id : '';

            $user_name2 = $request->user_name;

            $user_email = $request->user_email;
            $user_tel = $request->user_tel;
            $user_address = $request->user_address;

            $data = array(
                'user_name' => $user_name2,
                'user_email' => $user_email,
                'user_tel' => $user_tel,
                'user_address' => $user_address,
                'user_rand' => mt_rand(1000,9999),
            );
            $user_info = DB::table('user')->where("user_id", $user_id)->update($data);
            if ($user_info) {
                $user_info = DB::table('user') -> where("user_id",$user_id) -> first();
                return json_encode($user_info);
            }else {
                return 3;exit();
            }

        }
    }
}